//
// Created by 17775 on 2024/10/23.
//

#include "Time_Series.h"

void Time_Series::setFrameSpan(const int frames) const {
    bool bFrameSpanAvailable = false;
    RayCi_TimeSeries_Recording_Settings_isFrameSpanAvailable(_iTimeSeriesDocId, &bFrameSpanAvailable);
    if(bFrameSpanAvailable) {
        RayCi_TimeSeries_Recording_Settings_setFrameSpan(_iTimeSeriesDocId, frames);
    }
}

int Time_Series::getFrameSpan() const {
    int frames = 0;
    RayCi_TimeSeries_Recording_Settings_getFrameSpan(_iTimeSeriesDocId, &frames);
    return frames;
}

bool Time_Series::isFrameSpan() const {
    bool bFrameSpan = false;
    RayCi_TimeSeries_Recording_Settings_isFrameSpan(_iTimeSeriesDocId, &bFrameSpan);
    return bFrameSpan;
}

void Time_Series::setTimeSpan(const double dTime) const {
    bool bTimeSpanAvailable = false;
    RayCi_TimeSeries_Recording_Settings_isTimeSpanAvailable(_iTimeSeriesDocId, &bTimeSpanAvailable);
    if(bTimeSpanAvailable) {
        RayCi_TimeSeries_Recording_Settings_setTimeSpan(_iTimeSeriesDocId, dTime);
    }
}

double Time_Series::getTimeSpan() const {
    double dTimeSpan = 0;
    RayCi_TimeSeries_Recording_Settings_getTimeSpan(_iTimeSeriesDocId, &dTimeSpan);
    return dTimeSpan;
}

double Time_Series::getDelayTimeMin() const {
    double min = 0;
    RayCi_TimeSeries_Recording_Settings_getDelayTimeMin(_iTimeSeriesDocId, &min);
    return min;
}

void Time_Series::setDelayTime(double dDelay){
    bool bTimeSpanAvailable = false;
    RayCi_TimeSeries_Recording_Settings_isTimeSpanAvailable(_iTimeSeriesDocId, &bTimeSpanAvailable);
    if(const auto minDelay = getDelayTimeMin(); dDelay < minDelay) {
        emit setDelayMin(minDelay);
        dDelay = minDelay;
    }
    if(bTimeSpanAvailable) {
        RayCi_TimeSeries_Recording_Settings_setDelayTime(_iTimeSeriesDocId, dDelay);
    }
}

void Time_Series::setDelayTimeAlwaysMin() const {
    RayCi_TimeSeries_Recording_Settings_setDelayTimeAlwaysMin(_iTimeSeriesDocId);
}

void Time_Series::setMeasurementCount(const int value) const {
    RayCi_TimeSeries_Recording_Settings_setMeasurementCount(_iTimeSeriesDocId, value);
}

int Time_Series::getMeasurementCount() const {
    int iMeasurementCount = 0;
    RayCi_TimeSeries_Recording_Settings_getMeasurementCount(_iTimeSeriesDocId, &iMeasurementCount);
    return iMeasurementCount;
}

bool Time_Series::isFreeRunning() const {
    bool dFreeRunning = false;
    RayCi_TimeSeries_Recording_Settings_isFreeRunning(_iTimeSeriesDocId, &dFreeRunning);
    return dFreeRunning;
}

void Time_Series::setFreeRunning(const bool freeRunning) const {
    bool bFreeRunningAvailable = false;
    RayCi_TimeSeries_Recording_Settings_isFreeRunningAvailable(_iTimeSeriesDocId, &bFreeRunningAvailable);
    if(bFreeRunningAvailable) {
        RayCi_TimeSeries_Recording_Settings_setFreeRunning(_iTimeSeriesDocId, freeRunning);
    }
}

void Time_Series::setMedian(const bool bMedian) const {
    bool bMedianAvailable = false;
    RayCi_TimeSeries_Recording_Settings_isMedianAvailable(_iTimeSeriesDocId, &bMedianAvailable);
    if(bMedianAvailable) {
        RayCi_TimeSeries_Recording_Settings_setMedian(_iTimeSeriesDocId, bMedian);
    }
}

void Time_Series::setSingleShot(const bool bSingleShot) const {
    bool bSingleShotAvailable = false;
    RayCi_TimeSeries_Recording_Settings_isSingleShotAvailable(_iTimeSeriesDocId, &bSingleShotAvailable);
    if(bSingleShotAvailable) {
        RayCi_TimeSeries_Recording_Settings_setSingleShot(_iTimeSeriesDocId, bSingleShot);
    }
}

QString Time_Series::getRecordingRemark() {
    char szRemark_buffer[80] = {0};
    int nSize = sizeof(szRemark_buffer);
    bool success = RayCi_TimeSeries_Recording_getRemarkA(_iTimeSeriesDocId, szRemark_buffer, &nSize);
    return success ? QString::fromLatin1(szRemark_buffer) : QString();
}

void Time_Series::clearRecording() {
    RayCi_TimeSeries_Recording_clear(_iTimeSeriesDocId);
}

void Time_Series::stopRecording() {
    RayCi_TimeSeries_Recording_stop(_iTimeSeriesDocId);
}

// Sequence
int32_t Time_Series::getCurrentItem() const {
    int32_t iItemCount = 0;
    RayCi_TimeSeries_Sequence_getItemCount(_iTimeSeriesDocId, &iItemCount);
    int32_t iCurrentItem = 0;
    RayCi_TimeSeries_Sequence_getCurrentItem(_iTimeSeriesDocId, &iCurrentItem);
    char szInfo_buffer[80];
    char szPath_buffer[80];
    char szInfo_buffer1[80];
    char szPath_buffer1[80];
    int nSize = sizeof(szInfo_buffer);
    RayCi_TimeSeries_Sequence_Item_getInfoA(_iTimeSeriesDocId, 0, szInfo_buffer, &nSize);
    RayCi_TimeSeries_Sequence_Item_getPathNameA(_iTimeSeriesDocId, 0, szPath_buffer, &nSize);
    RayCi_TimeSeries_Sequence_Item_getInfoA(_iTimeSeriesDocId, 1, szInfo_buffer1, &nSize);
    RayCi_TimeSeries_Sequence_Item_getPathNameA(_iTimeSeriesDocId, 0, szPath_buffer1, &nSize); // C:\Users\177
    return iCurrentItem;
}

int32_t Time_Series::getItemCount() const {
    int32_t iItemCount = 0;
    RayCi_TimeSeries_Sequence_getItemCount(_iTimeSeriesDocId, &iItemCount);
    return iItemCount;
}

QString Time_Series::getItemInfo(int index) const {
    char szInfo_buffer[80] = {0};
    int nSize = sizeof(szInfo_buffer);
    RayCi_TimeSeries_Sequence_Item_getInfoA(_iTimeSeriesDocId, index, szInfo_buffer, &nSize);
    return QString::fromLatin1(szInfo_buffer);
}

void Time_Series::setCurrentItem(const int index) const {
    if(index < 0) return;
    RayCi_TimeSeries_Sequence_setCurrentItem(_iTimeSeriesDocId, index);
}

void Time_Series::getRefreshingState() const {
    bool bIsRefreshing = false;
    double dProgress = 0;
    RayCi_TimeSeries_Sequence_getRefreshingState(_iTimeSeriesDocId, &bIsRefreshing, &dProgress);
}

void Time_Series::refresh() const {
    RayCi_TimeSeries_Sequence_refresh(_iTimeSeriesDocId);
}

void Time_Series::exportReport() {
    RayCi_createDirectoryA("C:\\RayCi\\TimeSeries");
    OPENFILENAMEA ofn;
    char szFile[260] = { 0 };
    ZeroMemory(&ofn, sizeof(ofn));
    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = nullptr;
    ofn.lpstrFile = szFile;
    ofn.nMaxFile = sizeof(szFile) / sizeof(szFile[0]) - 1;
    ofn.lpstrFilter = "PDF Files(*.pdf)\0*.pdf*\0All Files(*.*)\0*.*\0\0";
    ofn.nFilterIndex = 1;
    ofn.lpstrInitialDir = "C:\\RayCi\\TimeSeries";
    ofn.lpstrTitle = "Save Report As";
    ofn.Flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;
    if (GetSaveFileNameA(&ofn) == TRUE) {
        const char* reportPathName = ofn.lpstrFile;
        std::string reportPathWithExtension = std::string(reportPathName) + ".pdf";
//        RayCi_TimeSeries_exportAsBeamStabilityA(_iTimeSeriesDocId, reportPathWithExtension.c_str(), true);
        RayCi_Single_Report_exportA(_iTimeSeriesDocId, reportPathWithExtension.c_str());
        ShellExecuteA(nullptr, "open", reportPathWithExtension.c_str(), nullptr, nullptr, SW_SHOWNORMAL);
    } else {
        MessageBoxA(nullptr, "Export was cancelled.", "Info", MB_OK | MB_ICONINFORMATION);
    }
}

void Time_Series::closeTimeSeriesWindow(const bool bWaitWhileClosing) const {
    RayCi_TimeSeries_close(_iTimeSeriesDocId, bWaitWhileClosing);
}
